##########################################################################################

library(data.table)
library(optparse)
library(ArchR)
library(Seurat)
library(ggplot2)

##########################################################################################
option_list <- list(
    make_option(c("--cell_cluster_file"), type = "character"),
    make_option(c("--motif_all_file"), type = "character"),
    make_option(c("--comine_data_file"), type = "character"),
    make_option(c("--out_path"), type = "character") 
)

if(1!=1){

    ## atac file
    cell_cluster_file <- "/public/home/xxf2019/20231121_singleMuti/results/celltype_plot/sperm_recluster/sperm_atac_cluster.tsv"

    ## motif的文件
    motif_all_file <- "~/20231121_singleMuti/results/celltype_plot/peak2gene/germ_mfuzz_new/Motif.Peak-Gene.positive.rds"

    ## rna的文件
    comine_data_file <- "~/20231121_singleMuti/results/qc_atac_v3/germ/testis_combined.annotationCellType.qc.Rdata"

    ## 输出
    out_path <- "/public/home/xxf2019/20231121_singleMuti/results/celltype_plot/sperm_recluster_rna/"

    #comine_data_all_file <- "~/20231121_singleMuti/results/qc_atac_v3/germ_peak-gene/testis_combined_peak.combineRNA.qc.Rdata"


}

###########################################################################################
parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

cell_cluster_file <- opt$cell_cluster_file
comine_data_file <- opt$comine_data_file
motif_all_file <- opt$motif_all_file
out_path <- opt$out_path

dir.create(out_path , recursive = T)

###########################################################################################

a <- load(comine_data_file)
b <- readRDS(motif_all_file)
c <- data.frame(fread(cell_cluster_file))

###########################################################################################

c$cell <- gsub( "#" , "_" , c$cell )
rownames(c) <- c$cell

###########################################################################################
set.seed(123)
scrnat_sperm <- subset(scrnat , cell_type=="Sperm")
scrnat_sperm <- FindNeighbors(scrnat_sperm, reduction = "pca", dims = 1:30)
scrnat_sperm <- FindClusters(scrnat_sperm , graph.name = grep( "snn" , names(scrnat_sperm@graphs) , value = T ) , resolution = 0.6)
scrnat_sperm$atac_recluster <- c[names(scrnat_sperm$cell),2]

out_file <- paste0( out_path , "/sperm.seurat_reclusters.rna.pdf" ) 
p <- DimPlot(scrnat_sperm, reduction = "umap", label = TRUE, group.by = 'seurat_clusters') + theme(legend.position = 'none') 
ggsave( out_file , p ,width = 6 , height = 6)

out_file <- paste0( out_path , "/sperm.atac_recluster.rna.pdf" ) 
p <- DimPlot(scrnat_sperm, reduction = "umap", label = TRUE, group.by = 'atac_recluster') + theme(legend.position = 'none') 
ggsave( out_file , p ,width = 6 , height = 6)

out_file <- paste0( out_path , "/sperm.reclusters.rna.Rdata" ) 
save( scrnat_sperm , file = out_file )